/*
 * TCPServer.java
 * TCP SERVER - OPENSTACK INSTANCE 
 * Vescape 2014
 */
package vescapecloud.network;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.json.JSONObject;

/**
 * Server TCP waiting for a TCP message from webService.
 *
 * @date 2014
 * @author Julie Dumons
 */
public class  TCPServer {

    // Client message 
    private static String clientSentence;

    private final static int TCP_PORT = 50501;

    // Number of maximum connections (if =0 : illimited) 
    private static int maxConnections = 0;

    //  Container
    private static JSONObject jsonObject;

   
    
    /**
     * Main thread to receive connection requests from clients
     *
     * @param args non used
     * @throws InterruptedException
     */
    public static void main(String[] args) throws InterruptedException {

        
        
        
        // number of clients connected
        int i = 0;

        // Open the connection socket
        try {
            
            ServerSocket sockWelcome = new ServerSocket(TCP_PORT);
            Socket connectionSocket;
            System.out.println("[TCPServer      ]     Socket Created");

            while ((i++ < maxConnections) || (maxConnections == 0)) {

                //  TCPHandleClient connection ;
                connectionSocket = sockWelcome.accept();
                System.out.println("[TCPServer      ]     Socket Accepted");
                System.out.println("[TCPServer      ]     New client n°"+i+"+connectionSocket.getInetAddress()");


                // Client thread creation
                TCPHandleClient connection_client = new TCPHandleClient(connectionSocket);
                Thread t = new Thread(connection_client);
                t.start();
                System.out.println("[TCPServer      ]     Thread client created");

            }
            sockWelcome.close();

        } catch (IOException ex) {
            Logger.getLogger(TCPServer.class.getName()).log(Level.SEVERE, null, ex);
        }

    }
    
    
    
    
    
}
